package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;





import org.apache.log4j.Logger;

import pojo.Book;
import pojo.User;
import dao.book.imp.BookDaoImp;
import dao.user.imp.UserDaoImp;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession sen = request.getSession();
		String psd = request.getParameter("psd");
		String uname = request.getParameter("uname");
		System.out.println(psd);
		System.out.println(uname);
		List<User> list = new UserDaoImp().findAll();
		System.out.println(list.size());
		for(User user:list){
			System.out.println(user.getName());
			System.out.println(user.getPassword());
			if(user.getName().equals(uname)&&user.getPassword().equals(psd)){
				Logger logger = Logger.getLogger(LoginServlet.class);
				logger.debug(uname+ " login ...");
				sen.setAttribute("uid", user.getId());
				sen.setAttribute("uname", uname);
				List<Book> books =new BookDaoImp().findAll();
				sen.setAttribute("books", books);
				response.sendRedirect("showbook.jsp");
				return;
			}
		}
		sen.setAttribute("error", "用户名或密码错误");
		response.sendRedirect("login.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
